<?php
namespace app\wxapp\controller;

use app\model\Record; 
use app\model\Util; 
use app\model\Ad; 
use app\model\Theme; 
use app\model\Sms; 
use app\model\Worker; 
use app\model\Fans; 
use app\model\Userdata; 
use app\model\Relation; 

class MysteryBoxesFeichi extends Base
{
    public function Mystery_boxes_feichi()
    {
global $_P, $_G;
$fans = $this->checkuser();
$system=$_G['system'];
$themeid = intval($_P['themeid']);
$theme = Theme::getThemeByIdxcx($themeid);
$config = Theme::getConfig($themeid);
$config = Theme::format_duoxuan($config);
if(empty($theme['id'])){
    wxapp_result(1, '活动入口不正确');
    exit;
}


$op = !empty($_P['op']) ? $_P['op'] : 'display';

//获取定单信息：theMyRecord
if ($config['maxcount']>0) {
    $recordid = intval($_P['recordid']);
}

extract(Record::getTheMyRecord($system,$fans,$recordid,$config,$themeid));
extract(Record::getShowRecord($system,$recordid,$config,$themeid));
if (Record::isFreeJoin($config,$themeid) && ($config['touxiang']==2 || $config['touxiang']==3)) {
    $config['touxiang']=1;
}
if ($op=='menu'){
    if (intval($_P['footmenu'])>0){
        Theme::add_scansum($themeid,intval($_P['workerid']));
    }
//    按扭文字处理
    $self = Ad::getSelfInfo($fans, $config, $themeid, $system);

    $mystery_boxes_yukuan = Fans::getMysteryboxesYukuanStatus($system,$config,$_G['openid'],$themeid);

    if ($config['yukuan'] > 0 && (TIMESTAMP > $config['yukuantime'] || $mystery_boxes_yukuan)) {
        $joinfee = $config['yukuan'];
    }else{
        $joinfee = $config['joinfee'];
    }
    $config['primeprice'] = str_replace(".00",'',$config['primeprice']);
    $config['groupprice'] = str_replace(".00",'',$config['groupprice']);
    $joinfee = str_replace(".00",'',$joinfee);
    $mystery_menu_ad1 = Theme::ButtonTextReplaceXCX($config['mystery_menu_ad1'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_ad2 = Theme::ButtonTextReplaceXCX($config['mystery_menu_ad2'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_right = Theme::ButtonTextReplaceXCX($config['mystery_menu_right'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_view_tuan = Theme::ButtonTextReplaceXCX($config['mystery_menu_view_tuan'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_view_boxs = Theme::ButtonTextReplaceXCX($config['mystery_menu_view_boxs'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_poster = Theme::ButtonTextReplaceXCX($config['mystery_menu_poster'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_view_tuan2 = Theme::ButtonTextReplaceXCX($config['mystery_menu_view_tuan2'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_share = Theme::ButtonTextReplaceXCX($config['mystery_menu_share'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_join_my_group = Theme::ButtonTextReplaceXCX($config['mystery_menu_join_my_group'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_join_group = Theme::ButtonTextReplaceXCX($config['mystery_menu_join_group'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_help = Theme::ButtonTextReplaceXCX($config['mystery_menu_help'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_join_group_boxs = Theme::ButtonTextReplaceXCX($config['mystery_menu_join_group_boxs'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_menu_view_group = Theme::ButtonTextReplaceXCX($config['mystery_menu_view_group'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_topay_new_group = Theme::ButtonTextReplaceXCX($config['mystery_topay_new_group'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_topay_join_group = Theme::ButtonTextReplaceXCX($config['mystery_topay_join_group'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_topay_new_user = Theme::ButtonTextReplaceXCX($config['mystery_topay_new_user'],$joinfee,$config['primeprice'],$config['groupprice']);
    $mystery_tuan_tips = Theme::ButtonTextReplaceXCX($config['mystery_tuan_tips'],$joinfee,$config['primeprice'],$config['groupprice']);

    if (strpos($mystery_menu_ad2,'.')!==false){
        $txtlen=mb_strlen($mystery_menu_ad2,'utf-8');
        if (mb_substr($mystery_menu_ad2, -1, 1, 'utf-8')=='0'){
            $mystery_menu_ad2 = mb_substr($mystery_menu_ad2, 0, $txtlen-1, 'utf-8');
        }
    }
    // 20联系客服
    // 21开新团
    // 22一键拼团
    // 23拼团进度
    // 24盲盒进度
    // 25全款支付
    // 26盲盒海报
    // 27返回首页
    // 28拼团海报
    // 29我的盲盒
    // 31重新填写表单：补余款
    // 32直接补余款
    // 33直接补余款
    // 34查看团排行
    //0:未报名
    //1：团未满：加入团
    //2：团已满：开新团
    //21：团已满：还可加入
    //3:查看自个的团状态2
    // 51查看团长排名详情
    // 52管理后台
    // 53我的海报
    // 54团长排名赛


    $tuan=array();
    $tuan['status'] = 0;
    $tuan['mystery_topay_tuan_tips'] = $mystery_tuan_tips;
    if ($config['mystery_is_preview']==1){
        $themyrecordid=1;
        $nowgroupneednum=0;
    }

    if (empty($themyrecordid) && !$mystery_boxes_yukuan){
        if (empty($showrecord)){
            $bottombut['left']['up']= $mystery_menu_ad1;
            $bottombut['left']['down']= $mystery_menu_ad2;
            $bottombut['right']['txt']= $mystery_menu_right;
            $bottombut['right']['url']= 21;
            $bottombut['topay']= $mystery_topay_new_user;
            $tuan['status'] = 0;
        }else{
            $bottombut['right']['txt']= $mystery_menu_join_group;
            $bottombut['right']['url']= 22;
            $bottombut['new_group']= $mystery_topay_new_group;
            $bottombut['join_group']= $mystery_topay_join_group;
            if ($nowgroupneednum>0){
                $tuan['status'] = 1;
                $bottombut['topay']= $mystery_topay_join_group;
            }else{
                if ($config['tuanmode']==1 || $config['tuanmode']==3){
                    $tuan['status'] = 21;
                    $bottombut['topay']= $mystery_topay_join_group;
                }elseif ($config['tuanmode']==0 || $config['tuanmode']==2){
                    $tuan['status'] = 2;
                    $bottombut['topay']= $mystery_topay_new_group;
                }
            }
        }

        if ($self['is_get_box']){
            $bottombut['left']['up'] = $mystery_menu_view_boxs;
            $bottombut['left']['url']= 24;
            $bottombut['left']['down']= '';

            $bottombut2['left']['txt'] = $mystery_menu_poster;
            $bottombut2['left']['url'] = 26;

            $bottombut2['right']['txt'] = $mystery_menu_share;
            $bottombut2['right']['url'] = 33;

            if (empty($showrecord)){
                $bottombut2['center']['txt']= $mystery_menu_right;
                $bottombut2['center']['url']= 21;
            }else{
                $bottombut2['center']['txt']= $mystery_menu_join_group;
                $bottombut2['center']['url']= 22;
            }
        }
        $bottombut['free']['txt'] = '免费报名拆盲盒';
    }
    else{
        //若开启补余款
        $orderpay = 0 ;
        // if (MODULE_NAME=='qa89628' || MODULE_NAME=='yx18900'){
            if ($config['yukuan'] > 0 && (TIMESTAMP > $config['yukuantime'] || $mystery_boxes_yukuan)) {
                $orderpay = Record::get_orderpay($config,$themeid);
                $yukuan_order = Record::getRecordYukuanByUid($config,$_G['member']['uid'],$themeid);
                if ($yukuan_order){
//                    if ($mystery_boxes_yukuan){
//                        if ($config['ishexiao']==1) {
//                            yxlog('3','飞驰盲盒');
//                            $bottombut['right']['txt'] = "查看我的定单";
//                            $bottombut['right']['url'] = 12;
//                        }else{
//                            $bottombut['right']['txt'] = "查看支付凭证";
//                            $bottombut['right']['url'] = 11;
//                        }
//                    }else{
                        $orderpay =0;
                        $yukuan_order = [];
//                    }
                }else{
                    if ($orderpay > 0) {
                        if (empty($config['presale']['menubtn_pay'])){
                            if (Record::isFreeJoin($config,$themeid)){
                                $config['presale']['menubtn_pay'] = '立即支付#fee元';
                            }else{
                                $config['presale']['menubtn_pay'] = '立即补余款#fee元';
                            }
                        }
                        $bottombut['right']['txt'] = str_replace("#fee",$orderpay,$config['presale']['menubtn_pay']) ;
                        $bottombut['right']['url'] = $config['presale']['ispresaleform']==1?31:32;
                    }
                }
            }
        // }else{
        //     if ($config['yukuan'] > 0 && (TIMESTAMP > $config['yukuantime'] || $mystery_boxes_yukuan)) {
        //         $orderpay = Record::get_orderpay($config,$themeid);
        //         $yukuan_order = Record::getRecordYukuanByUid($config,$_G['member']['uid'],$themeid);
        //         if ($yukuan_order){
        //             if ($config['ishexiao']==1) {
        //                 $bottombut['right']['txt'] = "查看我的定单";
        //                 $bottombut['right']['url'] = 12;
        //             }else{
        //                 $bottombut['right']['txt'] = "查看支付凭证";
        //                 $bottombut['right']['url'] = 11;
        //             }
        //         }else{
        //             if ($orderpay > 0) {
        //                 if (empty($config['presale']['menubtn_pay'])){
        //                     if (Record::isFreeJoin($config,$themeid)){
        //                         $config['presale']['menubtn_pay'] = '立即支付#fee元';
        //                     }else{
        //                         $config['presale']['menubtn_pay'] = '立即补余款#fee元';
        //                     }
        //                 }
        //                 $bottombut['right']['txt'] = str_replace("#fee",$orderpay,$config['presale']['menubtn_pay']) ;
        //                 $bottombut['right']['url'] = $config['presale']['ispresaleform']==1?31:32;
        //             }
        //         }
        //     }
        // }
        if ($orderpay<=0 && empty($yukuan_order)) {

            $bottombut['right']['txt'] = $mystery_menu_view_boxs;
            $bottombut['right']['url'] = 24;

//            if ($nowgroupneednum > 0 || $config['get_mystery_boxes'] == 2 || $config['get_mystery_boxes'] == 3) {
                if ($config['maxcountall'] > 1){
                    $bottombut['left']['up'] = $mystery_menu_view_tuan;
                    $bottombut['left']['url'] = 23;//23
                }else{
                    if (Record::isFreeJoin($config,$themeid)){
                        $bottombut['left']['up'] = '邀请好友参与';
                        $bottombut['left']['url'] = 28;
                    }elseif ($config['ishexiao']==1){
                        $bottombut['left']['up'] = "查看我的定单";
                        $bottombut['left']['url'] = 12;
                    }else{
                        $bottombut['left']['up'] = "查看支付凭证";
                        $bottombut['left']['url'] = 11;
                    }
                }

                $tuan['status'] = 3;
                if ($config['isautorefund'] == 0) {
                    $tuan['time'] = $theme['endtime'];
                    $tuan['time_txt'] = 0;
                } else {
                    $tuan['time'] = $themyrecord['addtime'] + $config['groupneedtime'] * 3600;
                    $tuan['time_txt'] = 1;
                }
                if ($nowgroupneednum == 0) {
                    $tuan['status'] = 4;
                }
//            }else {
//                $tuan['status'] = 4;
                //前端出现2个拆礼盒赢大奖，暂时关闭
//                if ($config['get_mystery_boxes'] != 1) {
//                    $bottombut['left']['up'] = '邀请好友参与';
//                    $bottombut['left']['url'] = 28;
//                } else {
//                    $bottombut['left']['up'] = $config['mystery_menu_view_boxs'];
//                    $bottombut['left']['url'] = 24;
//                }
//            }

            $tuan['time'] = $theme['endtime'];

            $bottombut2['left']['txt'] = $mystery_menu_poster;
            $bottombut2['left']['url'] = 26;

            $bottombut2['center']['txt'] = $mystery_menu_view_tuan2;
            $bottombut2['center']['url'] = 23;
            if ($config['mystery_boxs_isrank']==0){
                $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                if ($themyrecord['type']==0){
                    $bottombut2['share']['view_rank'] = '团长排名';
                }
            }
            $bottombut2['share']['txt'] = $mystery_menu_join_my_group;
            $bottombut2['right']['txt'] = $mystery_menu_share;
            $bottombut2['right']['url'] = 33;

            $bottombut2['tuan']['txt'] = '点击邀请好友';
            $bottombut2['tuan']['url'] = 33;

            if ($config['ishexiao']==1) {
                $bottombut['view_order']['txt'] = "查看我的定单";
                $bottombut['view_order']['url'] = 12;
            }else{
                $bottombut['view_order']['txt'] = "查看支付凭证";
                $bottombut['view_order']['url'] = 11;
            }
        }
    }
    $bottombut2['friend']['help'] = $mystery_menu_help;
    $bottombut2['friend']['help2'] = '点击帮我拆盲盒2';
    $bottombut2['friend']['join_group'] = $mystery_menu_join_group_boxs;
    if ($self['is_get_box']){
        $bottombut2['friend']['my'] = '查看我的'.$config['mystery_boxs_name'];
    }

    $tuan['time']= array(date("Y-m-d H:i:s", $tuan['time']));
    $tuan['neednum'] = $nowgroupneednum;
    $tuan['nowrecord']=$nowrecord;
    $tuan['groupneedtime']=$config['isautorefund'];
    //判断盲盒开启状态 引导图 未报名 已报名的判断 未报名只填写信息  已报名 填写信息 拉起支付
    $box_room_img = '';
    $box_to_pay = 0;
    $config_new = array(
        'mystery_boxes_left_color1' => $config['mystery_boxes_left_color1'],
        'mystery_boxes_left_color2' => $config['mystery_boxes_left_color2'],
        'mystery_boxes_right_color1' => $config['mystery_boxes_right_color1'],
        'mystery_boxes_right_color2' => $config['mystery_boxes_right_color2'],
        'mystery_boxes_left_font_color1' => $config['mystery_boxes_left_font_color1'],
        'mystery_boxes_left_font_color2' => $config['mystery_boxes_left_font_color2'],
        'mystery_boxes_right_font_color' => $config['mystery_boxes_right_font_color'],
        'mystery_boxes_customer_font_color' => $config['mystery_boxes_customer_font_color'],
        'mystery_boxes_customer_background_color' => $config['mystery_boxes_customer_background_color'],
        'mystery_boxes_right_font_size' => $config['mystery_boxes_right_font_size'].'rpx',
        'mystery_boxes_right_font_bold' => $config['mystery_boxes_right_font_bold'],
        'mystery_is_lump_sump' => $config['mystery_is_lump_sump'],
        'mystery_form_top' => $config['mystery_form_top'],
        'mystery_group_tips1' => $config['mystery_group_tips1'],
        'mystery_group_tips2' => $config['mystery_group_tips2'],
        'mystery_group_tips3' => $config['mystery_group_tips3'],
        'mystery_boxes_help_color' => empty($config['mystery_boxes_help_color'])?'#0023a1':$config['mystery_boxes_help_color'],
        'mystery_boxs_name' => $config['mystery_boxs_name'],
        'friend_finish'=>'好友助力已完成',
        'but_friend_help'=>'帮TA拆盲盒',
        'but_friend_help_success'=>'已帮TA拆盲盒',
        'but_friend_help_my'=>'我的盲盒',
        'but_friend_help_no_reg'=>'我也拆盲盒',
    );

    if ($config['get_mystery_boxes']==2 || $config['get_mystery_boxes']==3){
        $config_new['mystery_boxes_room_img'] = ($fans['username'] && $fans['click_open_box'])?'':$config['box_room_img'];
        if ($config['get_mystery_boxes']==3){
            $config_new['mystery_boxes_modal_to_pay'] = 1;
        }

        $config_new['myboxs_btn'] = array(
            'text' => $config['mystery_my_join_text'],
            'share' => $config['mystery_my_share_text'],
            'height' => $config['mystery_my_join_height'],
            'color' => $config['mystery_my_join_color'],
            'font_color' => $config['mystery_my_join_text_color'],
        );
    }
    if ($_P['example']!=1) {
        $config_new['isworker'] = Worker::Isworker($_G['member']['uid'], $themeid, 1);
    }else{
        $bottombut2['left']['txt'] = $mystery_menu_poster;
        $bottombut2['left']['url'] = 27;

        $bottombut2['center']['txt'] = $mystery_menu_view_tuan2;
        $bottombut2['center']['url'] = 27;

        $bottombut2['share']['txt'] = $mystery_menu_join_my_group;
        $bottombut2['right']['txt'] = $mystery_menu_share;
        $bottombut2['right']['url'] = 27;

        $bottombut2['tuan']['txt'] = '点击邀请好友';
        $bottombut2['tuan']['url'] = 27;
    }

//    fromuid="{{friend && op!='my'?friend.fromuid:0}}"

    if ($_P['fromuid']!=$_G['member']['uid'] && !empty($_P['fromuid'])){
        $friend = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
        $gift_list = Ad::getAddressGift($themeid,'show');
        $gift_list=array_chunk($gift_list, 3);
        if ($config['get_mystery_boxes']==2){
            $friend['newuser']=30;
        }elseif (!empty($bottombut['right']['url'])){
            $friend['newuser'] = $bottombut['right']['url'];
        }else{
            $friend['newuser']=21;
        }
//        if ($friend['is_friend_help'] || $friend['is_finish_friend']){
//            $friend=array();
//        }
    }

    // 51查看团长排名详情
    // 52管理后台
    // 53我的海报
    // 34团长排名赛

    if ($config_new['isworker'] && $config['mystery_boxs_isrank_worker']==1){
        $bottombut['left']['up']='管理后台';
        $bottombut['left']['url']=52;
        if ($config['mystery_boxs_isrank']==0){
            $bottombut['right']['txt']='团长排名赛';
            $bottombut['right']['url']=34;
        }else{
            $bottombut['right']['txt']='我的活动海报';
            $bottombut['right']['url']=53;
        }
    }
    if ($theme['status']==2 && $config['isshow_end_footermenu']!=1){
        if (empty($themyrecordid)){
            $bottombut['left']['txt'] = '';
            $bottombut['left']['url'] = '';
            $bottombut['center']['txt']= '';
            $bottombut['center']['url']= '';
            $bottombut['right']['txt']= '活动已结束';
            $bottombut['right']['url']= 998;
        }
    }
    if ($theme['begintime']>time()){
        $bottombut['left']['txt'] = '';
        $bottombut['left']['url'] = '';
        $bottombut['center']['txt']= '';
        $bottombut['center']['url']= '';
        $bottombut['right']['txt']= '活动尚未开始';
        $bottombut['right']['url']= 997;
    }

    $result = array(
        'bottombut'=>$bottombut,
        'bottombut2'=>$bottombut2,
        'tuan'=>$tuan,
        'config'=>$config_new,
        'friend'=>$friend,
        'gift_list'=>$gift_list,
        'endtime'=>array(date("Y-m-d H:i:s", $theme['endtime'])),
    );
    wxapp_result(0, '',$result);
}
else if ($op=='display'){
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    //获取当前用户
    if ($self['is_get_box']){
        $mystery_page_lump_sump_text = Theme::ButtonTextReplaceXCX($config['mystery_page_lump_sump_text'],$config['joinfee'],$config['primeprice'],$config['groupprice']);
        $mystery_page_lump_sump_text = str_replace("#all",$config['mystery_lump_sump'].'元',$mystery_page_lump_sump_text);
        $mystery_page_lump_sump_text=str_replace(".00","",$mystery_page_lump_sump_text);

        $isbukuan = db_find ( 'SELECT * FROM ' . tablename ( t_record ) . " as r
                WHERE r.themeid ='{$themeid}' and uid = '{$_G['member']['uid']}' and  r.type = 8 and r.status > 0" );

        if (!empty($isbukuan)){
            $bottombut2_url = $config['ishexiao']==1?11:12;
            $mystery_page_lump_sump_text = str_replace("#all",$isbukuan['fee'].'元',$config['mystery_page_lump_sump_success_text']);
        }else{
            $bottombut2_url = 25;
        }
        if ($config['get_mystery_boxes']==1){
            $page_button=array(
                'lump_sump'=>array(
                    'text'=>$mystery_page_lump_sump_text,
                    'height'=>$config['mystery_page_lump_sump_height'],
                    'color'=>$config['mystery_page_lump_sump_color'],
                    'font_color'=>$config['mystery_page_lump_sump_font_color'],
                    'url' => $bottombut2_url,
                ),
                'friend'=>array(
                    'text'=>$config['mystery_page_friend_text'],
                    'success'=>$config['mystery_page_friend_success_text'],
                    'height'=>$config['mystery_page_friend_height'],
                    'color'=>$config['mystery_page_friend_color'],
                    'font_color'=>$config['mystery_page_friend_font_color'],
                ),
                'avatar'=>array(
                    'size'=>$config['mystery_page_avatar_size'],
                    'height'=>$config['mystery_page_avatar_height'],
                    'font_color'=>$config['mystery_page_avatar_color'],
                ),
                'myboxs_btn'=>array(
                    'text'=>$config['mystery_my_join_text'],
                    'share'=>$config['mystery_my_share_text'],
                    'height'=>$config['mystery_my_join_height'],
                    'color'=>$config['mystery_my_join_color'],
                    'font_color'=>$config['mystery_my_join_text_color'],
                ),
            );
            if ($config['mystery_is_lump_sump']==0){
                $page_button['lump_sump']['height']=-300;
            }
        }
    }

    if ($system['yx_coupon']==1){
        
        require_once YX_PLUGIN_CORE . 'inc/plugin_model.php';
        require_once get_path('plugin/coupon/core/model.php');
        $xieyi = \CouponModel::getUserUseCouponTips($themeid,$config,$system,$_G['member']['uid']);
    }else{
        $xieyi = '';
    }
    $config_new=array(
        'nametext'    => $config['nametext'],
        'mobiletext'    => $config['mobiletext'],
        'datetime'    => $config['datetime'],
        'datetimetype'    => $config['datetimetype'],
        'addinfo'    => $config['addinfo'],
        'addinfotype'    => $config['addinfotype'],
        'address'    => $config['address'],
        'addresstype'    => $config['addresstype'],
        'addressoptions'    => $config['addressoptions'],
        "formtwo" =>$config['formtwo'],
        "formtwotype" =>$config['formtwotype'],
        "formtwooptions" =>$config['formtwooptions'],
        'shops'    => $config['shops'],
        'shopsmin'    => $config['shopsmin'],
        'shopsmax'    => $config['shopsmax'],
        'shopstype'    => $config['shopstype'],
        'options2'    => $config['options2'],
        'gudingoptions_txt'    => $config['gudingoptions_txt'],
        'shopsoptions'    => $config['options'],
        'gudingoptions'    => $config['gudingoptions'],
        'shopstype5'    => $config['shopstype5'],
        "xieyi" =>$xieyi?$xieyi:$config['xieyi'],

        "getmobile" =>$config['getmobile'],
        "getavatar" =>$config['getavatar'],
        'pageshare' => $config['pageshare'],

        'mystery_friend_need'=>$config['mystery_friend_need'],
        'mystery_boxes_content_top_xcx' => Theme::format_jpg($config['mystery_boxes_content_top_xcx'],$fans,$theme,$system),
        'mystery_boxes_content_bottom_xcx' => Theme::format_jpg($config['mystery_boxes_content_bottom_xcx'],$fans,$theme,$system),
        'time'=>array(date("Y-m-d H:i:s", $theme['endtime'])),
        'but_friend_help'=>'帮TA拆盲盒',
        'but_friend_help_success'=>'已帮TA拆盲盒',
        'but_friend_help_success_no_reg'=>'已帮TA拆盲盒,我也拆盲盒',
        'but_self_no_reg'=>'点击我也拆盲盒',
        'but_self_reg'=>'查看自己的进度',
        'but_self_share'=>'点击邀请好友拆盲盒',
        'but_self_open_boxs'=>'立即拆盲盒',
        'friend_help_success'=>'感谢你帮我拆',
        'friend_help_success2'=>'幸运盲盒！',
        'friend_finish'=>'通知TA拆盲盒',
        'but_self_success'=>'联系我们【领取礼品】',
        'mystery_show_time'=>$config['mystery_show_time'],
        'mystery_friend_mode4'=>$config['mystery_friend_mode4'],
        'mystery_friend_mode5'=>$config['mystery_friend_mode5'],
        'mystery_gift_autoplay' => $config['mystery_gift_autoplay'],
        'get_mystery_boxes' => $config['get_mystery_boxes'],

        'mystery_boxes_feichi_color' => $config['mystery_boxes_feichi_color'],
        'mystery_boxs_name' => $config['mystery_boxs_name'],
        'mystery_gift_select' => $config['mystery_gift_select'],
    );

    $isworker = Worker::Isworker($_G['member']['uid'],$themeid,1);
    $config['isrank'] = $config['isrank']==1 || $isworker['level'] ==1 ?1:0;

    if ($_P['fromuid']!=$_G['member']['uid'] && !empty($_P['fromuid'])){
        $friend = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
    }
    if ($config['mystery_is_preview']==1){
        $nowrecord = array(
            "tips"=>"此团还差1人 立即加入",
            "zhang"=>array(
                "avatar"=>$fans['avatar'],
                "username"=>mb_substr($fans['nickname'],0,4,'utf-8'),
            ),
            "detail"=>array(
                0=>array(
                    "img"=>"https://css2020.yixuan.net/2020css/fcpdd/images/ask.png",
                    "username"=>"",
                ),
            )
        );
    }
    //判断A访问B，A已帮B助力，且A也报名则显示A
//    if ($friend && $friend['is_friend_help']){
//        if ($fans['click_open_box']){
//            $friend = [];
//        }
//    }

    if ($config['get_mystery_boxes']==2){
        $config_new['friend_status_tips'] = $friend['username'];
    }elseif ($config['get_mystery_boxes']==1){
        $config_new['friend_status_tips'] = '拼团成功';
    }elseif ($config['get_mystery_boxes']==3){
        $config_new['friend_status_tips'] = '抢购成功';
    }
    if (!empty($_P['page_op'])){
        $friend_detail=array();
        if ($_P['page_op']=='my' || empty($friend)){
            if (!empty($_G['member']['uid'])){
                $params[':fromuid'] = $_G['member']['uid'];
                $params[':themeid'] = $themeid;
                $sql = "SELECT s.nickname,s.avatar,s.uid,s.username,d.uid,d.fromuid,d.addtime 
                        FROM " . tablename ( t_friend ) . " as d 
                        left join " . tablename ( t_fans ) . " as s on s.uid = d.uid and s.themeid=d.themeid 
                        WHERE d.themeid = :themeid and d.fromuid = :fromuid order by d.addtime";
                $friend_detail['list'] = db_select ( $sql,$params);
                $friend_detail['op']='my';
                $friend_detail['title']=$self['username']."的".$config['mystery_boxs_name'];
                $friend_detail['need_friend_num']=$self['need_friend_num'];
                $friend_detail['tips']="喜欢哪个就点他";
                if ($self['need_friend_num']>0){
                    $friend_detail['tips'] .="	，还差".$self['need_friend_num']."位好友助力，即可解锁任意一个礼盒";
                }
            }
        }
        elseif(!empty($friend)){
            $params[':fromuid'] = $friend['fromuid'];
            $params[':themeid'] = $themeid;
            $sql = "SELECT s.nickname,s.avatar,s.uid,s.username,d.uid,d.fromuid,d.addtime 
                    FROM " . tablename ( t_friend ) . " as d 
                    left join " . tablename ( t_fans ) . " as s on s.uid = d.uid and s.themeid=d.themeid and s.uniacid=d.uniacid
                    WHERE d.themeid = :themeid and d.fromuid = :fromuid order by d.addtime";
            $friend_detail['list'] = db_select ( $sql,$params);
            $friend_detail['op']='friend';
            $friend_detail['title']=$friend['username']."的".$config['mystery_boxs_name'];
            $friend_detail['need_friend_num']=$friend['need_friend_num'];
            if ($friend['is_finish_friend']){
                $friend_detail['tips'] = $friend['username'] ."已完成好友助力，等待TA打开".$config['mystery_boxs_name']."~";
            }else{
                $friend_detail['tips'] = $friend['username'] ."还差".$friend['need_friend_num']."位好友助力，即可成功打开";
                if (count($friend['gift'])){
                    $friend_detail['tips'] .= "新" .$config['mystery_boxs_name'];
                }else{
                    $friend_detail['tips'] .= $config['mystery_boxs_name'];
                }
            }

        }

        if (!empty($friend_detail['list'])) {
            foreach ($friend_detail['list'] as $index => $row) {
                $friend_detail['list'][$index]['nickname'] = mb_substr($row['nickname'], 0, 5, 'utf-8');
            }
        }
    }

    //分享朋友圈
    $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
    if (empty($myworker) ){
        if(!empty($themyrecord)){
            $workerid = $themyrecord['workerid'];
        }else{
            $workerid = intval($_P['workerid']);
        }
        if ($workerid==0){
            $relation = Relation::getByThemeId($themeid,$fans['openid']);
            $workerid = $relation['workerid'];
        }
        $theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
        $workername = $theyworker['username'];
    }else{
        $workername = $myworker['username'];
    }
    if (!empty($config['mystery_sharetitle'])){
        $config['sharetitlexcx']=$config['mystery_sharetitle'];
        $config['sharetitlexcx2']=$config['mystery_sharetitle'];
    }
    if (!empty($config['mystery_shareimg'])){
        $config['shareimgxcx']=$config['mystery_shareimg'];
    }
    $shareinfo = Theme::getshareinfo($system,$theme,$config,$fans,'',$workername,$recordid,$_P['fromuid']);


    if ($_P['example']==1){
        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
        if (!empty($myworker)){
            $gift = Ad::getAddressGift($themeid,'show');
            $xuni = Fans::getXuniFans($config,1,1,1);

            $self = array(
                'avatar'=>$xuni[0]['avatar'],
                'username'=>$xuni[0]['username'],
                'is_finish_friend'=> true,
                'is_get_box'=> true,
                'is_open_boxs'=> true,
                'mystery_process_show'=> 0,
                'mystery_process_show_small_img'=> "https://css2020.yixuan.net/2020css/mystery_boxes/small_img.png",
                'need_friend_num'=>null,
                'gift'=>$gift[rand(0,count($gift)-1)],
                'is_zhifu'=>true,
                'is_pingtuan'=>true,
                'mystery_gift_select'=>0,
                'is_select_gift'=>true,
            );

            if ($config['mystery_gift_select']!=1){
                if (!empty($config['mystery_friend_mode3'])){
                    $open_num = 3;
                }elseif (!empty($config['mystery_friend_mode2'])){
                    $open_num = 2;
                }else{
                    $open_num = 1;
                }
            }else{
                $open_num = 1;
            }
            if ($open_num==1){
                $self['gifts'][0]=$gift[rand(0,count($gift)-1)];
            }else{
                $gifts_key =array_rand ($gift, $open_num);
                foreach ($gifts_key as $item){
                    $self['gifts'][]=$gift[$item];
                }
            }
            $friend_detail['title']=$self['username']."的".$config['mystery_boxs_name'];

            $mystery_friend_need=$config['mystery_friend_need']+$config['mystery_friend_need2']+$config['mystery_friend_need3'];
            $friend_detail['list'] = Fans::getXuniFans($config,$mystery_friend_need,0,0,rand(1,600));
            if (!empty($friend_detail['list'])) {
                foreach ($friend_detail['list'] as $index => $row) {
                    $friend_detail['list'][$index]['nickname'] = mb_substr($row['username'], 0, 5, 'utf-8');
                }
            }
            $friend_detail['op']='my';
            $friend_detail['tips']="喜欢哪个就点他";
            $friend_detail['need_friend_num']=0;
        }
    }

    $result = array(
        'config'=>$config_new,
        'page_button'=>$page_button,
        'shareinfo'=>$shareinfo,
        'nowrecord'=>$nowrecord,
        'friend_detail'=>$friend_detail,
        'friend'=>$friend,
        'self'=>$self,
        'gift'=>Ad::getAddressGift($themeid,'show'),
        'boxs_top_imgs'=>$config['boxs_top_imgs'],
        'boxs_bottom_imgs'=>$config['boxs_bottom_imgs'],
    );
    $result['jishu']=Worker::getjishu($theme,$system,$config['pinpailogo']);
    wxapp_result(0, '',$result);
}
else if ($op=='get_myboxs'){
    $params = array();
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    if ($_P['fromuid']!=$_G['member']['uid'] && !empty($_P['fromuid'])){
        $friend = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
        $params[':fromuid'] = $_P['fromuid'];
    }else{
        $params[':fromuid'] = $_G['member']['uid'];
    }
    $params[':themeid'] = $themeid;
    $friendlist = db_select("SELECT avatar,nickname FROM ".tablename(t_friend)."
                        WHERE themeid = :themeid and fromuid = :fromuid order by id desc" ,$params);


    if ($system['yx_coupon']==1){
        
        require_once YX_PLUGIN_CORE . 'inc/plugin_model.php';
        require_once get_path('plugin/coupon/core/model.php');
        $xieyi = \CouponModel::getUserUseCouponTips($themeid,$config,$system,$_G['member']['uid']);
    }else{
        $xieyi = '';
    }
    $config_new=array(
        'nametext'    => $config['nametext'],
        'mobiletext'    => $config['mobiletext'],
        'datetime'    => $config['datetime'],
        'datetimetype'    => $config['datetimetype'],
        'addinfo'    => $config['addinfo'],
        'addinfotype'    => $config['addinfotype'],
        'address'    => $config['address'],
        'addresstype'    => $config['addresstype'],
        'addressoptions'    => $config['addressoptions'],
        'shops'    => $config['shops'],
        'shopsmin'    => $config['shopsmin'],
        'shopsmax'    => $config['shopsmax'],
        'shopstype'    => $config['shopsmax'],
        'options'    => $config['shopsmax'],
        'gudingoptions'    => $config['shopsmax'],
        'shopstype5'    => $config['shopsmax'],
        "xieyi" =>$xieyi?$xieyi:$config['xieyi'],
        'pageshare'=>$config['pageshare'],
    );
    $result = array(
        'self'=>$self,
        'friendlist'=>$friendlist,
        'config'=>$config_new,
    );
    wxapp_result(0, '',$result);
}
else if ($op=='get_rank'){
    //已经废弃
    $r = Record::getrank($system,$theme,$config,$themeid,true);
    if ($r['errcode']==1){
        wxapp_result(1, $r['msg']);
    }else{
        $result = $r['data'];
        $result['jishu']=Worker::getjishu($theme,$system,$config['pinpailogo']);
        wxapp_result(0, '',$result);
    }
}
else if ($op=='open_boxs'){
    if ($config['mystery_boxes_endtime_set']==1 && $config['mystery_boxes_endtime']<TIMESTAMP || $config['mystery_boxes_endtime_set']==0 && $theme['status'] == 2){
        wxapp_result(1, '活动已结束');
    }
    if (empty($_G['member']['uid'])){
        wxapp_result(1, 'UID参数错误');
    }
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    if (!$self['is_get_box']){
        wxapp_result(1, '未达到拆盲盒条件');
    }
    if (!$self['is_finish_friend']){
        wxapp_result(1, '还差' . $self['need_friend_num'].'位好友助力即可解锁任意一个礼盒，快去邀请好友！');
    }
    if ($self['is_open_boxs']){
        wxapp_result(1, '盲盒已开启');
    }else{
        $result = Ad::getAddressGift($themeid, 'get');
        if (empty($result)){
            wxapp_result(1, '盲盒礼品未配置');
        }
        if ($config['get_mystery_boxes']==2){
            $formuid_record = Record::getRecordByUidNopay($_G['member']['uid'],$themeid);
        }else{
            $formuid_record = Record::getRecordByUid($_G['member']['uid'],$themeid);
        }

        $beizhu = date('Y-m-d H:i:s').' 拆开宝盒 获得【' . $result['name'] . '】 操作:' . $_G['clientip'] ;
        Userdata::addLog($themeid, $_G['member']['uid'], 29,$beizhu);
        if (!empty($formuid_record['address'])){
            $giftname = explode('|', $formuid_record['address']);
            if ($config['mystery_gift_get']==0){
                $giftname = array_flip($giftname);
                $giftname = array_flip($giftname);
            }
            $giftname = implode('|',$giftname) . "|";
        }else{
            $giftname='';
        }
        $data=array(
            'address' => $giftname . $result['name'],
            'beizhu' => $formuid_record['beizhu'].$beizhu. '<br>',
        );

        db_update(t_record, $data, array('id' => $formuid_record['id']));
        $temp = $self['gifts'];
        $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
        $self['gift'] = Ad::getFansBoxGift($config, $result['name']);
        $self['gifts'] = $temp;
        $self['gifts_back'] = Ad::getFansBoxGiftMult($config, $data['address']);
    }

    $result = array(
        'self'=>$self,
    );
    wxapp_result(0, '',$result);
}
else if ($op=='select_gifts'){
    if (empty($_G['member']['uid'])){
        wxapp_result(1, 'UID参数错误');
    }
    $giftName = $_P['giftName'];
    if (empty($giftName)){
        wxapp_result(1, '请选择礼品');
    }
    $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    if (!$self['is_get_box']){
        wxapp_result(1, '未获得盲盒');
    }
    if (!$self['is_finish_friend']){
        wxapp_result(1, '邀请好友助力未完成');
    }
    if (count($self['gifts'])<2){
        wxapp_result(1, '盲盒礼品参数错误');
    }
    if ($self['is_select_gift']){
        wxapp_result(1, '盲盒礼品已选择');
    }else{
        foreach ($self['gifts'] as $index => $row) {
            if ($giftName==$row['name']){
                $select_gift = $row['name'];
            }
        }
        if (empty($select_gift)){
            wxapp_result(1, '礼品参数错误：未找到所选礼品');
        }else{
            if ($config['get_mystery_boxes']==2){
                $formuid_record = Record::getRecordByUidNopay($_G['member']['uid'],$themeid);
            }else{
                $formuid_record = Record::getRecordByUid($_G['member']['uid'],$themeid);
            }
            $beizhu = date('Y-m-d H:i:s').' 选择礼品【' . $select_gift . '】 操作:' . $_G['clientip'] ;
            Userdata::addLog($themeid, $_G['member']['uid'], 30,$beizhu);
            $data=array(
                'address' => $formuid_record['address'] ."|选中".$select_gift,
                'beizhu' => $formuid_record['beizhu'].$beizhu. '<br>',
            );
            db_update(t_record, $data, array('id' => $formuid_record['id']));
        }
        $self = Ad::getSelfInfo($fans,$config,$themeid,$system);
    }
    $result = array(
        'self'=>$self,
    );
    wxapp_result(0, '',$result);
}
else if ($op=='friend_help'){
    if ($config['mystery_boxes_endtime_set']==1 && $config['mystery_boxes_endtime']<TIMESTAMP || $config['mystery_boxes_endtime_set']==0 && $theme['status'] == 2){
        wxapp_result(1, '活动已结束');
    }
    if (empty($_G['member']['uid'])){
        wxapp_result(1, 'UID参数错误');
    }
    if ($_P['fromuid']!=$_G['member']['uid'] && !empty($_P['fromuid'])){
        $friend = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
    }else{
        wxapp_result(1, '助力失败：不可给自己助力');
    }
    if (!$friend['is_get_box']){
        wxapp_result(1, '助力失败：TA未获得盲盒');
    }
    if ($friend['is_finish_friend']){
        wxapp_result(1, '助力失败：TA的助力已完成');
    }
    $is_friend_help = Record::isFriendHelp($_G['member']['uid'],$_P['fromuid'],$themeid);
    if ($friend['is_friend_help'] || $is_friend_help){
        wxapp_result(1, '助力失败：已帮TA助力过');
    }
    db_run('set AUTOCOMMIT=0');//关闭自动提交，自此句执行以后，每个SQL语句或者语句块所在的事务都需要显示"commit"才能提交事务
    db_run('START TRANSACTION');//启动一个新事务
    $data = array(
        'themeid'   => $themeid,
        'uniacid'   => $_G['mpid'],
        'uid'       => $_G['member']['uid'],
        'openid'    => $_G['openid'],
        'status'    => 1,
        'addtime'   => TIMESTAMP,
        'fromuid'  => $_P['fromuid'],
    );

    if (!empty($_P['onlyinfo'])){
        if (isset($_P['username']) && $_P['username'] && isset($_P['mobile']) && $_P['mobile']){
            $pm = array(":themeid"=>$themeid,":uid"=>$_G['member']['uid']);
            db_query("UPDATE ".tablename(t_fans)." SET username = '{$_P['username']}',mobile = '{$_P['mobile']}' WHERE themeid =:themeid and uid = :uid",$pm);
            $fans['username'] = $_P['username'];
            $fans['mobile'] = $_P['mobile'];
        }
    }
    $r1 = $insertid = db_insert(t_friend, $data);


    $pm = array(":themeid"=>$themeid,":uid"=>$_P['fromuid']);
    $r2 = db_query("UPDATE ".tablename(t_fans)." SET friendnum = friendnum +1 WHERE themeid =:themeid and uid = :uid",$pm);

    if (!$r1 || !$r2) {
        db_run('ROLLBACK');//事务回滚
        wxapp_result(1, '助力失败：未知错误');
    }else{
        Userdata::addLog($themeid, $_G['member']['uid'], 31,'',0,$_P['fromuid']);
        db_run('COMMIT');//事务提交
    }

    $friend_new = Ad::getFriendInfo($_P['fromuid'],$config,$themeid,$system);
    if ($friend_new['is_finish_friend']==true && $friend_new['is_finish_friend']!=$friend['is_finish_friend']){
        Util::send_mb_help_success($system,$config,$theme,$_P['fromuid']);
        $fans['success_mode']=1;
        Sms::CheckAndSendSms('mystery_boxes_new',$system,$config,$themeid, $fans,$data);
    }else{
        Sms::CheckAndSendSms('friend_help',$system,$config,$themeid, $fans,$data);
    }
    $friend = $friend_new;
    $friend_detail=array();
    $params[':fromuid'] = $friend['fromuid'];
    $params[':themeid'] = $themeid;
    $sql = "SELECT s.nickname,s.avatar,s.uid,s.username,d.uid,d.fromuid,d.addtime 
                    FROM " . tablename ( t_friend ) . " as d 
                    left join " . tablename ( t_fans ) . " as s on s.uid = d.uid and s.themeid=d.themeid 
                    WHERE d.themeid = :themeid and d.fromuid = :fromuid order by d.addtime";
    $friend_detail['list'] = db_select ( $sql,$params);
    if (!empty($friend_detail['list'])) {
        foreach ($friend_detail['list'] as $index => $row) {
            $friend_detail['list'][$index]['nickname'] = mb_substr($row['nickname'], 0, 5, 'utf-8');
        }
    }
    $friend_detail['op']='friend';
    $friend_detail['title']=$friend['username']."的".$config['mystery_boxs_name'];
    $friend_detail['need_friend_num']=$friend['need_friend_num'];

    $friend['mystery_help_img'] = $config['mystery_help_img'];
    $friend['mystery_help_img_width'] = $config['mystery_help_img_width'];
    $result = array(
        'friend_detail'=>$friend_detail,
        'friend'=>$friend,
    );
    $result['ad_info'] = Ad::get_ad_info($config,$fans,$themeid,'','wxapp','friendhelp');
    wxapp_result(0, '',$result);
}

}
}